home *** CD-ROM | disk | FTP | other *** search
- Imports System.Threading
- Imports System.Text.RegularExpressions
- Imports System.Net
- Imports System.Web.Services.Protocols
-
- Public Class Form1
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.IContainer
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- Friend WithEvents btnAbort As System.Windows.Forms.Button
- Friend WithEvents btnTestSession As System.Windows.Forms.Button
- Friend WithEvents btnAsyncCall As System.Windows.Forms.Button
- Friend WithEvents btnTimeout As System.Windows.Forms.Button
- Friend WithEvents btnAsyncMulti As System.Windows.Forms.Button
- Friend WithEvents btnPermanentSession As System.Windows.Forms.Button
- Friend WithEvents btnCaching As System.Windows.Forms.Button
- Friend WithEvents btnException As System.Windows.Forms.Button
- Friend WithEvents btnWindowsAuthentication As System.Windows.Forms.Button
- Friend WithEvents btnSoapHeaders As System.Windows.Forms.Button
- Friend WithEvents btnCustomAuthentication As System.Windows.Forms.Button
- Friend WithEvents btnOneWay As System.Windows.Forms.Button
- Friend WithEvents btnSoapExtensions As System.Windows.Forms.Button
- Friend WithEvents lblMessage As System.Windows.Forms.Label
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.btnTestSession = New System.Windows.Forms.Button()
- Me.btnAbort = New System.Windows.Forms.Button()
- Me.lblMessage = New System.Windows.Forms.Label()
- Me.btnAsyncCall = New System.Windows.Forms.Button()
- Me.btnTimeout = New System.Windows.Forms.Button()
- Me.btnAsyncMulti = New System.Windows.Forms.Button()
- Me.btnPermanentSession = New System.Windows.Forms.Button()
- Me.btnCaching = New System.Windows.Forms.Button()
- Me.btnException = New System.Windows.Forms.Button()
- Me.btnWindowsAuthentication = New System.Windows.Forms.Button()
- Me.btnSoapHeaders = New System.Windows.Forms.Button()
- Me.btnCustomAuthentication = New System.Windows.Forms.Button()
- Me.btnOneWay = New System.Windows.Forms.Button()
- Me.btnSoapExtensions = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'btnTestSession
- '
- Me.btnTestSession.Location = New System.Drawing.Point(16, 264)
- Me.btnTestSession.Name = "btnTestSession"
- Me.btnTestSession.Size = New System.Drawing.Size(120, 40)
- Me.btnTestSession.TabIndex = 0
- Me.btnTestSession.Text = "Test session state"
- '
- 'btnAbort
- '
- Me.btnAbort.Location = New System.Drawing.Point(16, 72)
- Me.btnAbort.Name = "btnAbort"
- Me.btnAbort.Size = New System.Drawing.Size(120, 40)
- Me.btnAbort.TabIndex = 1
- Me.btnAbort.Text = "Abort method"
- '
- 'lblMessage
- '
- Me.lblMessage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
- Me.lblMessage.Location = New System.Drawing.Point(296, 24)
- Me.lblMessage.Name = "lblMessage"
- Me.lblMessage.Size = New System.Drawing.Size(280, 312)
- Me.lblMessage.TabIndex = 2
- '
- 'btnAsyncCall
- '
- Me.btnAsyncCall.Location = New System.Drawing.Point(16, 120)
- Me.btnAsyncCall.Name = "btnAsyncCall"
- Me.btnAsyncCall.Size = New System.Drawing.Size(120, 40)
- Me.btnAsyncCall.TabIndex = 3
- Me.btnAsyncCall.Text = "Asynchronous Call"
- '
- 'btnTimeout
- '
- Me.btnTimeout.Location = New System.Drawing.Point(16, 24)
- Me.btnTimeout.Name = "btnTimeout"
- Me.btnTimeout.Size = New System.Drawing.Size(120, 40)
- Me.btnTimeout.TabIndex = 4
- Me.btnTimeout.Text = "Test Timeout"
- '
- 'btnAsyncMulti
- '
- Me.btnAsyncMulti.Location = New System.Drawing.Point(16, 168)
- Me.btnAsyncMulti.Name = "btnAsyncMulti"
- Me.btnAsyncMulti.Size = New System.Drawing.Size(120, 40)
- Me.btnAsyncMulti.TabIndex = 5
- Me.btnAsyncMulti.Text = "Multiple Async calls"
- '
- 'btnPermanentSession
- '
- Me.btnPermanentSession.Location = New System.Drawing.Point(144, 24)
- Me.btnPermanentSession.Name = "btnPermanentSession"
- Me.btnPermanentSession.Size = New System.Drawing.Size(120, 40)
- Me.btnPermanentSession.TabIndex = 6
- Me.btnPermanentSession.Text = "Permanent Sessions"
- '
- 'btnCaching
- '
- Me.btnCaching.Location = New System.Drawing.Point(144, 72)
- Me.btnCaching.Name = "btnCaching"
- Me.btnCaching.Size = New System.Drawing.Size(120, 40)
- Me.btnCaching.TabIndex = 7
- Me.btnCaching.Text = "Test Caching"
- '
- 'btnException
- '
- Me.btnException.Location = New System.Drawing.Point(144, 120)
- Me.btnException.Name = "btnException"
- Me.btnException.Size = New System.Drawing.Size(120, 40)
- Me.btnException.TabIndex = 8
- Me.btnException.Text = "Test Exception"
- '
- 'btnWindowsAuthentication
- '
- Me.btnWindowsAuthentication.Location = New System.Drawing.Point(144, 216)
- Me.btnWindowsAuthentication.Name = "btnWindowsAuthentication"
- Me.btnWindowsAuthentication.Size = New System.Drawing.Size(120, 40)
- Me.btnWindowsAuthentication.TabIndex = 9
- Me.btnWindowsAuthentication.Text = "Windows Authentication"
- '
- 'btnSoapHeaders
- '
- Me.btnSoapHeaders.Location = New System.Drawing.Point(144, 168)
- Me.btnSoapHeaders.Name = "btnSoapHeaders"
- Me.btnSoapHeaders.Size = New System.Drawing.Size(120, 40)
- Me.btnSoapHeaders.TabIndex = 10
- Me.btnSoapHeaders.Text = "SOAP Headers"
- '
- 'btnCustomAuthentication
- '
- Me.btnCustomAuthentication.Location = New System.Drawing.Point(144, 264)
- Me.btnCustomAuthentication.Name = "btnCustomAuthentication"
- Me.btnCustomAuthentication.Size = New System.Drawing.Size(120, 40)
- Me.btnCustomAuthentication.TabIndex = 11
- Me.btnCustomAuthentication.Text = "Custom authentication"
- '
- 'btnOneWay
- '
- Me.btnOneWay.Location = New System.Drawing.Point(16, 216)
- Me.btnOneWay.Name = "btnOneWay"
- Me.btnOneWay.Size = New System.Drawing.Size(120, 40)
- Me.btnOneWay.TabIndex = 12
- Me.btnOneWay.Text = "One-way method calls"
- '
- 'btnSoapExtensions
- '
- Me.btnSoapExtensions.Location = New System.Drawing.Point(144, 312)
- Me.btnSoapExtensions.Name = "btnSoapExtensions"
- Me.btnSoapExtensions.Size = New System.Drawing.Size(120, 40)
- Me.btnSoapExtensions.TabIndex = 13
- Me.btnSoapExtensions.Text = "Test Soap Extensions"
- '
- 'Form1
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(600, 357)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSoapExtensions, Me.btnOneWay, Me.btnCustomAuthentication, Me.btnSoapHeaders, Me.btnWindowsAuthentication, Me.btnException, Me.btnCaching, Me.btnPermanentSession, Me.btnAsyncMulti, Me.btnTimeout, Me.btnAsyncCall, Me.lblMessage, Me.btnAbort, Me.btnTestSession})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "Form1"
- Me.Text = "Advanced Web Services"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- Dim service As New localhost.SampleService()
-
- ' test the Timeout property
-
- Private Sub btnTimeout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimeout.Click
- Try
- ' Set a timeout of 5 seconds.
- service.Timeout = 5000
- ' Invoke a method that takes 10 seconds to complete.
- service.LengthyMethodCall(10)
- Catch ex As SoapException
- lblMessage.Text = "SOAP Exception: " & ex.message
- Catch ex As WebException
- If ex.Status = WebExceptionStatus.Timeout Then
- ' The operation timed out.
- lblMessage.Text = "TIMEOUT: " & ex.message
- Else
- lblMessage.Text = ex.Message
- End If
- Catch ex As Exception
- ' Another exception has occurred.
- lblMessage.Text = "Exception: " & ex.Message
- End Try
- End Sub
-
- ' test the Abort method - note that you must call the WS from
- ' another thread if you want to abort the call
-
- Private Sub btnAbort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbort.Click
- ' run the web service from another thread.
- Dim tr As New Thread(AddressOf CallWebMethod)
- tr.Start()
- ' wait for 5 seconds, then kill the method call.
- Thread.Sleep(5000)
- service.Abort()
- End Sub
-
- ' this proc calls the WS method from another thread
-
- Sub CallWebMethod()
- Try
- ' make a lengthy call
- service.LengthyMethodCall(10)
- Catch ex As WebException
- If ex.Status = WebExceptionStatus.RequestCanceled Then
- ' the call has been aborted
- lblMessage.Text = ex.Message
- End If
- End Try
- End Sub
-
- ' test asynchronous WS method calls
-
- Private Sub btnAsyncCall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAsyncCall.Click
- Dim service As New localhost.SampleService()
- Dim ar As IAsyncResult = service.BeginLengthyMethodCall(2, AddressOf MethodCallback, service)
- End Sub
-
- ' this method is called when the WS method call completes
-
- Sub MethodCallback(ByVal ar As IAsyncResult)
- Dim service As localhost.SampleService = _
- DirectCast(ar.AsyncState, localhost.SampleService)
- service.EndLengthyMethodCall(ar)
- lblMessage.Text = "End of the lengthy method call"
- End Sub
-
- ' call multiple WS methods
-
- Private Sub btnAsyncMulti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAsyncMulti.Click
- ' Call the web service methods from another thread.
- Dim tr As New Thread(AddressOf CallMultipleWebMethods)
- tr.Start()
- End Sub
-
- ' this proc is where multiple SW methods are called
-
- Sub CallMultipleWebMethods()
- ' run two WS methods asynchronously
- Dim service1 As New localhost.SampleService()
- Dim ar1 As IAsyncResult = service1.BeginLengthyMethodCall(2, Nothing, Nothing)
-
- Dim service2 As New localhost.SampleService()
- Dim ar2 As IAsyncResult = service2.BeginLengthyMethodCall(3, Nothing, Nothing)
-
- lblMessage.Text = "Two method calls have started" & ControlChars.CrLf
- ' wait for 3 seconds.
- Thread.Sleep(3000)
-
- ' set the next constant to True to abort the async method call
- ' if it hasn't completed yet
- #Const ABORT_WSMETHODS = False
-
- #If ABORT_WSMETHODS Then
- If ar1.IsCompleted Then
- ' if method completed, get the result
- Dim res As Integer = service1.EndLengthyMethodCall(ar1)
- lblMessage.Text &= "First method has been completed" & controlchars.CrLf
- Else
- ' else, get the WebClientAsyncResult object and call its Abort method
- Dim wcar1 As WebClientAsyncResult = DirectCast(ar1, WebClientAsyncResult)
- wcar1.Abort()
- lblMessage.Text &= "First method has been canceled" & controlchars.CrLf
- End If
-
- If ar2.IsCompleted Then
- ' if method completed, get the result
- Dim res As Integer = service2.EndLengthyMethodCall(ar2)
- lblMessage.Text = "Second method call has been completed" & controlchars.CrLf
- Else
- ' else, get the WebClientAsyncResult object and call its Abort method
- Dim wcar2 As WebClientAsyncResult = CType(ar2, WebClientAsyncResult)
- wcar2.Abort()
- lblMessage.Text &= "Second method call has been canceled" & controlchars.CrLf
- End If
- ' nothing else to do in this case
- Exit Sub
- #End If
-
- ' Wait until both methods complete.
- Dim handleArr() As WaitHandle = {ar1.AsyncWaitHandle, ar2.AsyncWaitHandle}
- WaitHandle.WaitAll(handleArr)
- lblMessage.Text &= "Both methods have been completed"
-
- ' complete the calls
- service1.EndLengthyMethodCall(ar1)
- service2.EndLengthyMethodCall(ar2)
- End Sub
-
- ' test how you can have Session-based web services
- ' each time you click on this button the displayed value is incremented
-
- Private Sub btnTestSession_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestSession.Click
- ' make the proxy class a cookie container
- If service.CookieContainer Is Nothing Then
- service.CookieContainer = New CookieContainer()
- End If
- ' prove that each call increments this counter
- lblMessage.Text = service.IncrementCounter()
- End Sub
-
- ' test one-way WS calls
-
- Private Sub btnOneWay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOneWay.Click
- ' call a one-way method call.
- Dim service As New localhost.SampleService()
- lblMessage.Text = ""
- ' call the One-way method, no need to complete the call
- service.OneWayLengthyMethodCall(10)
- lblMessage.Text = "Method call started"
- End Sub
-
- ' this proc demonstrates that clients can share a WS state if
- ' they share the same session cookie
-
- Dim saveCookie As Cookie
-
- Private Sub btnPermanentSession_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPermanentSession.Click
- ' make the proxy object a cookie container
- Dim service As New localhost.SampleService()
- service.CookieContainer = New CookieContainer()
-
- ' If we already know the cookie, let's add it to the cookies collection.
- If Not (saveCookie Is Nothing) Then
- service.CookieContainer.Add(saveCookie)
- End If
-
- ' Call the Web Service method.
- lblMessage.Text = service.IncrementCounter()
-
- ' Save the cookie if this is the first call.
- If saveCookie Is Nothing Then
- Dim cookieUri As New Uri("http://localhost")
- ' Get the ASP.NET_SessionID cookie belonging to the localhost URI.
- saveCookie = service.CookieContainer.GetCookies(cookieUri).Item("ASP.NET_SessionId")
- End If
- End Sub
-
- ' test cached WS calls
- ' By clicking on this button multiple times, you'll see that the
- ' time returned doesn't change at each call, because the WS result
- ' is cached
-
- Private Sub btnCaching_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCaching.Click
- Dim service As New localhost.SampleService()
- lblMessage.Text = service.GetTime(1)
- lblMessage.Text &= " " & service.GetTimeCounter.ToString
- End Sub
-
- ' call a WS method that returns an exception
-
- Private Sub btnException_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnException.Click
- Try
- ' this call throws an exception
- Dim service As New localhost.SampleService()
- service.ThrowAnException()
-
- Catch ex As SoapException When ex.Code.Equals(SoapException.VersionMismatchFaultCode)
-
- Catch ex As SoapException When ex.Code.Equals(SoapException.ServerFaultCode)
- lblMessage.Text = "SOAP Exception: " & ex.Message
-
- Catch ex As SoapException When GetWSException(ex) = "System.NullReferenceException"
- ' get details from the OuterXml property
- lblMessage.Text = ex.Detail.OuterXml
- End Try
- End Sub
-
- ' A reusable routine that extracts the name of the "inner" exception.
-
- Function GetWSException(ByVal ex As SoapException) As String
- ' parse the exception's Message property.
- Dim mc As MatchCollection = Regex.Matches(ex.Message, "---> ([^:]+):")
- If mc.Count >= 1 Then
- ' we've found a match - the first ground contains the value.
- Return mc.Item(0).Groups(1).Value
- End If
- End Function
-
- ' this procedure shows how you can make a WS method call
- ' and impersonate a given Windows account
-
- ' NOTE: you need to edit web.config to enforce Windows authentication
-
- Private Sub btnWindowsAuthentication_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWindowsAuthentication.Click
- ' create network credentials.
- Dim winCred As New NetworkCredential()
- winCred.UserName = "Administrator"
- winCred.Password = "fbxx"
- ' invoke a method, passing the user credentials.
- Dim service As New localhost.SampleService()
- service.Credentials = winCred
- service.LengthyMethodCall(0)
- End Sub
-
- ' this proc tests passing a SOAP header to the WS method
-
- Private Sub btnSoapHeaders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSoapHeaders.Click
- ' create a SOAP header
- Dim userInfo As New localhost.UserInfoHeader()
- userInfo.Culture = "it-it"
- userInfo.TimeOffset = 1
- ' assign the SOAP header to the proper property in the proxy object
- Dim service As New localhost.SampleService()
- service.UserInfoHeaderValue = userInfo
- ' call the remote method
- lblMessage.Text = service.GetClientTime
- End Sub
-
- ' this procedure shows how you can set up a custom authentication
- ' mechanism based on SOAP headers
-
- Private Sub btnCustomAuthentication_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustomAuthentication.Click
- ' create a SOAP header
- Dim accountInfo As New localhost.AccountInfoHeader()
- accountInfo.UserName = "JoeDoe"
- accountInfo.Password = "jdpwd"
-
- Try
- ' associate account info with the proxy object.
- Dim service As New localhost.SampleService()
- service.AccountInfoHeaderValue = accountInfo
- ' This call will succeed.
- Dim res As Boolean = service.ProtectedMethod()
- ' this one will fail
- res = service.AnotherProtectedMethod
-
- Catch ex As Exception
- MsgBox(ex.Message, MsgBoxStyle.Critical)
- End Try
- End Sub
-
- ' this proc tests SOAP extensions
-
- Private Sub btnSoapExtensions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSoapExtensions.Click
- ' prepare a SOAP header with user info
- Try
- ' create a SOAP header that identifies a user with higher privileges
- Dim accountInfo As New localhost.AccountInfoHeader()
- accountInfo.UserName = "AnnSmith"
- accountInfo.Password = "aspwd"
- ' associate account info with the proxy object.
- Dim service As New localhost.SampleService()
- service.AccountInfoHeaderValue = accountInfo
-
- ' This call will succeed.
- lblMessage.Text = "FIRST CALL " & ControlChars.CrLf
- Dim res As Boolean = service.YetAnotherProtectedMethod()
- lblMessage.Text &= res.ToString & ControlChars.CrLf & ControlChars.CrLf
-
- ' now repeat the call with the identity of a user with
- ' fewer privileges
- accountInfo.UserName = "JoeDoe"
- accountInfo.Password = "jdpwd"
- lblMessage.Text &= "SECOND CALL " & ControlChars.CrLf
- ' this call will fail
- res = service.YetAnotherProtectedMethod()
-
- Catch ex As Exception
- lblMessage.Text &= ex.Message
- End Try
- End Sub
- End Class
-